home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / make.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  23KB  |  663 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. /* last modified 03/11/98 */
  7. /*
  8. ********************************************************************************
  9. *                                                                              *
  10. *   MakeMove() is responsible for updating the position database whenever a    *
  11. *   piece is moved.  it performs the following operations:  (1) update the     *
  12. *   board structure itself by moving the piece and removing any captured       *
  13. *   piece.  (2) update the hash keys.  (3) update material counts.  (4) update *
  14. *   castling status.  (5) update number of moves since last reversible move.   *
  15. *                                                                              *
  16. ********************************************************************************
  17. */
  18. void MakeMove(TREE *tree, int ply, int move, int wtm)
  19. {
  20.   register int piece, from, to, captured, promote;
  21.   BITBOARD bit_move;
  22. /*
  23.  ----------------------------------------------------------
  24. |                                                          |
  25. |   first, clear the EnPassant_Target bit-mask.  moving a  |
  26. |   pawn two ranks will set it later in MakeMove().        |
  27. |                                                          |
  28.  ----------------------------------------------------------
  29. */
  30. #if defined(DEBUG)
  31.   ValidatePosition(tree,ply,move,"MakeMove(1)");
  32. #endif
  33.   tree->position[ply+1]=tree->position[ply];
  34.   tree->save_hash_key[ply]=HashKey;
  35.   tree->save_pawn_hash_key[ply]=PawnHashKey;
  36.   if (EnPassant(ply+1)) {
  37.     HashEP(EnPassant(ply+1),HashKey);
  38.     EnPassant(ply+1)=0;
  39.   }
  40.   Rule50Moves(ply+1)++;
  41. /*
  42.  ----------------------------------------------------------
  43. |                                                          |
  44. |   now do the piece-specific things by calling the        |
  45. |   appropriate routine.                                   |
  46. |                                                          |
  47.  ----------------------------------------------------------
  48. */
  49.   piece=Piece(move);
  50.   from=From(move);
  51.   to=To(move);
  52.   captured=Captured(move);
  53.   promote=Promote(move);
  54. MakePieceMove:
  55.   ClearRL90(from,OccupiedRL90);
  56.   ClearRL45(from,OccupiedRL45);
  57.   ClearRR45(from,OccupiedRR45);
  58.   SetRL90(to,OccupiedRL90);
  59.   SetRL45(to,OccupiedRL45);
  60.   SetRR45(to,OccupiedRR45);
  61.   bit_move=Or(set_mask[from],set_mask[to]);
  62.   PieceOnSquare(from)=0;
  63.   switch (piece) {
  64. /*
  65. ********************************************************************************
  66. *                                                                              *
  67. *   make pawn moves.  there are two special cases:  (a) enpassant captures     *
  68. *   where the captured pawn is not on the "to" square and must be removed in   *
  69. *   a different way, and (2) pawn promotions (where the "Promote" variable     *
  70. *   is non-zero) requires updating the appropriate bit boards since we are     *
  71. *   creating a new piece.                                                      *
  72. *                                                                              *
  73. ********************************************************************************
  74. */
  75.   case pawn:
  76.     if (wtm) {
  77.       ClearSet(bit_move,WhitePawns);
  78.       ClearSet(bit_move,WhitePieces);
  79.       HashPW(from,HashKey);
  80.       HashPW32(from,PawnHashKey);
  81.       HashPW(to,HashKey);
  82.       HashPW32(to,PawnHashKey);
  83.       PieceOnSquare(to)=pawn;
  84.       if (captured == 1) {
  85.         if(!And(BlackPawns,set_mask[to])) {
  86.           ClearRL90(to-8,OccupiedRL90);
  87.           ClearRL45(to-8,OccupiedRL45);
  88.           ClearRR45(to-8,OccupiedRR45);
  89.           Clear(to-8,BlackPawns);
  90.           Clear(to-8,BlackPieces);
  91.           HashPB(to-8,HashKey);
  92.           HashPB32(to-8,PawnHashKey);
  93.           PieceOnSquare(to-8)=0;
  94.           Material+=PAWN_VALUE;
  95.           TotalBlackPawns--;
  96.           TotalPieces--;
  97.           captured=0;
  98.         }
  99.       }
  100.   /*
  101.    --------------------------------------------------------------------
  102.   |                                                                    |
  103.   |  if this is a pawn promotion, remove the pawn from the counts      |
  104.   |  then update the correct piece board to reflect the piece just     |
  105.   |  created.                                                          |
  106.   |                                                                    |
  107.    --------------------------------------------------------------------
  108.   */
  109.       if (promote) {
  110.         TotalWhitePawns--;
  111.         Material-=PAWN_VALUE;
  112.         Clear(to,WhitePawns);
  113.         HashPW(to,HashKey);
  114.         HashPW32(to,PawnHashKey);
  115.         switch (promote) {
  116.         case knight:
  117.           Set(to,WhiteKnights);
  118.           HashNW(to,HashKey);
  119.           PieceOnSquare(to)=knight;
  120.           TotalWhitePieces+=knight_v;
  121.           WhiteMinors++;
  122.           Material+=KNIGHT_VALUE;
  123.           break;
  124.         case bishop:
  125.           Set(to,WhiteBishops);
  126.           Set(to,BishopsQueens);
  127.           HashBW(to,HashKey);
  128.           PieceOnSquare(to)=bishop;
  129.           TotalWhitePieces+=bishop_v;
  130.           WhiteMinors++;
  131.           Material+=BISHOP_VALUE;
  132.           break;
  133.         case rook:
  134.           Set(to,WhiteRooks);
  135.           Set(to,RooksQueens);
  136.           HashRW(to,HashKey);
  137.           PieceOnSquare(to)=rook;
  138.           TotalWhitePieces+=rook_v;
  139.           WhiteMajors++;
  140.           Material+=ROOK_VALUE;
  141.           break;
  142.         case queen:
  143.           Set(to,WhiteQueens);
  144.           Set(to,BishopsQueens);
  145.           Set(to,RooksQueens);
  146.           HashQW(to,HashKey);
  147.           PieceOnSquare(to)=queen;
  148.           TotalWhitePieces+=queen_v;
  149.           WhiteMajors+=2;
  150.           Material+=QUEEN_VALUE;
  151.           break;
  152.         }
  153.       }
  154.       else 
  155.         if (((to-from) == 16) && And(mask_eptest[to],BlackPawns)) {
  156.           EnPassant(ply+1)=to-8;
  157.           HashEP(to-8,HashKey);
  158.         }
  159.     }
  160.     else {
  161.       ClearSet(bit_move,BlackPawns);
  162.       ClearSet(bit_move,BlackPieces);
  163.       HashPB(from,HashKey);
  164.       HashPB32(from,PawnHashKey);
  165.       HashPB(to,HashKey);
  166.       HashPB32(to,PawnHashKey);
  167.       PieceOnSquare(to)=-pawn;
  168.       if (captured == 1) {
  169.         if(!And(WhitePawns,set_mask[to])) {
  170.           ClearRL90(to+8,OccupiedRL90);
  171.           ClearRL45(to+8,OccupiedRL45);
  172.           ClearRR45(to+8,OccupiedRR45);
  173.           Clear(to+8,WhitePawns);
  174.           Clear(to+8,WhitePieces);
  175.           HashPW(to+8,HashKey);
  176.           HashPW32(to+8,PawnHashKey);
  177.           PieceOnSquare(to+8)=0;
  178.           Material-=PAWN_VALUE;
  179.           TotalWhitePawns--;
  180.           TotalPieces--;
  181.           captured=0;
  182.         }
  183.       }
  184. /*
  185.  --------------------------------------------------------------------
  186. |                                                                    |
  187. |  if this is a pawn promotion, remove the pawn from the counts      |
  188. |  then update the correct piece board to reflect the piece just     |
  189. |  created.                                                          |
  190. |                                                                    |
  191.  --------------------------------------------------------------------
  192. */
  193.       if (promote) {
  194.         TotalBlackPawns--;
  195.         Material+=PAWN_VALUE;
  196.         Clear(to,BlackPawns);
  197.         HashPB(to,HashKey);
  198.         HashPB32(to,PawnHashKey);
  199.         switch (promote) {
  200.         case knight:
  201.           Set(to,BlackKnights);
  202.           HashNB(to,HashKey);
  203.           PieceOnSquare(to)=-knight;
  204.           TotalBlackPieces+=knight_v;
  205.           BlackMinors++;
  206.           Material-=KNIGHT_VALUE;
  207.           break;
  208.         case bishop:
  209.           Set(to,BlackBishops);
  210.           Set(to,BishopsQueens);
  211.           HashBB(to,HashKey);
  212.           PieceOnSquare(to)=-bishop;
  213.           TotalBlackPieces+=bishop_v;
  214.           BlackMinors++;
  215.           Material-=BISHOP_VALUE;
  216.           break;
  217.         case rook:
  218.           Set(to,BlackRooks);
  219.           Set(to,RooksQueens);
  220.           HashRB(to,HashKey);
  221.           PieceOnSquare(to)=-rook;
  222.           TotalBlackPieces+=rook_v;
  223.           BlackMajors++;
  224.           Material-=ROOK_VALUE;
  225.           break;
  226.         case queen:
  227.           Set(to,BlackQueens);
  228.           Set(to,BishopsQueens);
  229.           Set(to,RooksQueens);
  230.           HashQB(to,HashKey);
  231.           PieceOnSquare(to)=-quee